package com.example.mvplogin.login;

import android.content.Context;

import com.example.mvplogin.api.WanAndroidApi;
import com.example.mvplogin.bean.LoginAndRegisterBean;
import com.example.mvplogin.network.ApiClient;

import org.reactivestreams.Subscriber;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * model提供数据
 */
public class LoginModel {

    /**
     * 登录
     * @param username  登录账号
     * @param password  登录密码
     * @param loginIpl  登录的回调接口
     */
    public void login(String username, String password, final LoginIpl loginIpl, Context context){
        WanAndroidApi wanAndroidApi = ApiClient.apiClient.createRetrofit(WanAndroidApi.class);
        Observable<LoginResponseWrapper<LoginResponse>> loginResponseWrapperObservable = wanAndroidApi.loginT(username, password);
        loginResponseWrapperObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ResponseObserver<LoginResponse>(context,true) {

            @Override
            public void success(LoginResponse loginResponse) {
                loginIpl.LoginSuccess(loginResponse);
            }

            @Override
            public void fail(String errorCode) {
                loginIpl.LoginFail();
            }
        });

    }
}
